package com.flyme.module.base.provider.listener;

import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.module.base.client.entity.SysSlider;
import com.flyme.module.base.client.event.SliserAddEvent;
import com.flyme.module.base.provider.service.SysSliderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class SliderAddListener implements SmartApplicationListener {


    @Autowired
    private SysSliderService sliderService;

    /**
     * 只有监听类型匹配才执行
     */
    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventClass) {
        return eventClass == SliserAddEvent.class;
    }

    /**
     * 只有在SysCourseServiceImpl内发布的事件时才会执行下面逻辑
     */
    @Override
    public boolean supportsSourceType(Class<?> aClass) {
        return true;
    }

    /**
     * 业务逻辑
     */
    @Override
    @Async
    public void onApplicationEvent(ApplicationEvent applicationEvent) {

        // 转换事件类型
        SliserAddEvent sliserAddEvent = (SliserAddEvent) applicationEvent;
        // 获取注册用户对象信息
        String moduleType = sliserAddEvent.getModuleType();
        Long moduleId = sliserAddEvent.getModuleId();
        String sliderTitle = sliserAddEvent.getSliderTitle();
        String sliderImg = sliserAddEvent.getSliderImg();
        CriteriaQuery q = new CriteriaQuery<SysSlider>();
        //先删除
        sliderService.remove(q.eq("moduleType", moduleType).eq("moduleId", moduleId));
        SysSlider slider=new SysSlider();
        slider.setModuleId(moduleId);
        slider.setModuleType(moduleType);
        slider.setSliderImg(sliderImg);
        slider.setSliderType(1);
        slider.setSliderTitle(sliderTitle);
        slider.setJumpType(2);
        slider.setSendStatus(CommonConstants.DISABLED);
        sliderService.save(slider);
    }

    /**
     * 同步情况下监听执行的顺序
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
